<?php
declare (strict_types = 1);

namespace app\admin\controller;

use app\BaseController;
use app\middleware\admin\Auth;
use app\model\Users;
use think\facade\Db;


class Member extends BaseController
{
    protected $middleware=[Auth::class];
    public function index()
    {
        $keyword=request()->param('keyword');
       $data= Users::when($keyword,function ($query) use($keyword){
            $query->where('username','like',"%{$keyword}%");
        })->order('id','desc')->paginate(10);
        return $this->success($data);
    }


    public function create()
    {
       return $this->success(Users::create(request()->param()));
    }


    public function update($id)
    {
        $params=request()->param();

        if (!empty($params['integral_ing']) && $params['integral_ing']>0) {
            $str = '積分加'.$params['integral_ing'];
        } else {
            $str = '積分減'.$params['integral_ing'];
        }
        Db::table('admin_log')->insert([
            'type'=>2,
            'admin_id'=>request()->userid,
            'content'=>$str,
            'created_at'=>time()
        ]);
        return $this->success(Users::where('id',$id)->update(request()->param()));
    }


    public function delete($id)
    {
        return $this->success(Users::where('id',$id)->delete());
    }
}
